<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="矩阵名称" prop="name">
				<el-input v-model="form.name" placeholder="矩阵名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="矩阵描述" prop="description">
				<el-input v-model="form.description" placeholder="矩阵描述" clearable :rows="5" type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button type="primary" :loading="isSaving" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增矩阵',
					edit: '编辑矩阵'
				},
				form: {
					id:"",
					name: "",
					description: "",
				},
				rules: {
					name:[
						{required: true, message: '请填写矩阵名称'}
					]
				},
				visible: false,
				isSaving: false,
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this;
			},
			submit(){
				this.$refs.dialogForm.validate(async(valid) => {
					if (valid) {
						this.isSaving = true;
						var res;
						if(this.mode == 'add')res = await this.$API.system.matrix.save.post(this.form);
						else if(this.mode == 'edit')res = await this.$API.system.matrix.edit.post(this.form);
						if(res.success){
							this.visible = false;
							this.$message.success("操作成功")
							this.$emit('success', this.form, this.mode)
						}
						this.isSaving = false;
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.description = data.description
				this.form.name = data.name
			}
		}
	}
</script>

<style>
</style>
